package com.lm.shop.order.rule;

import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.Server;

import java.util.List;

/**
 * @author lsk
 * @version 1.0.0
 * @ClassName MyRule.java
 * @Description 自定义负载均衡
 * @createTime 2022年10月25日 14:10:00
 */
public class MyRule implements IRule {

    private ILoadBalancer iLoadBalancer;

    @Override
    public Server choose(Object o) {
        final List<Server> allServers = this.iLoadBalancer.getAllServers();
        int index = (int)(+Math.random()*(allServers.size() -1));
        return allServers.get(index);
    }

    @Override
    public void setLoadBalancer(ILoadBalancer iLoadBalancer) {
        iLoadBalancer = iLoadBalancer;
    }

    @Override
    public ILoadBalancer getLoadBalancer() {
        return iLoadBalancer;
    }
}
